package com.mdd.admin.controller.common;

import com.alibaba.fastjson.JSONObject;
import com.mdd.admin.service.common.IIndexService;
import com.mdd.common.config.GlobalConfig;
import com.mdd.common.core.AjaxResult;
import com.mdd.common.utils.HttpUtil;
import com.mdd.common.utils.IpUtil;
import com.mdd.common.utils.RequestUtil;
import com.mdd.common.utils.YmlUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 主页管理
 */
@Slf4j
@RestController
@RequestMapping("api/common/index")
public class IndexController {

    @Resource
    IIndexService iIndexService;

    /**
     * 控制台
     *
     * @author fzr
     * @return AjaxResult
     */
    @GetMapping("/console")
    public AjaxResult console() {
        Map<String, Object> map = iIndexService.console();
        return AjaxResult.success(map);
    }

    /**
     * 公共配置
     *
     * @author fzr
     * @return AjaxResult
     */
    @GetMapping("/config")
    public AjaxResult config() {
        Map<String, Object> map = iIndexService.config();
        return AjaxResult.success(map);
    }

    @GetMapping("/version/productAuth")
    public AjaxResult verifyAuthorization() {
        String code = YmlUtil.get("like.verify-version.code");
        String server = YmlUtil.get("like.verify-version.server");
        HttpServletRequest request = RequestUtil.handler();
        String domain = request.getServerName();
        String params = "code=" + code + "&domain=" + domain;
        String json = HttpUtil.sendGet(server + "/api/version/productAuth", params);
        return AjaxResult.success(JSONObject.parseObject(json));
    }

    @GetMapping("/version/hasNew")
    public AjaxResult hasNew() {
        String code = YmlUtil.get("like.verify-version.code");
        String server = YmlUtil.get("like.verify-version.server");
        String version = GlobalConfig.version;
        String params = "code=" + code + "&domain=" + version;
        String json = HttpUtil.sendGet(server + "/api/version/hasNew", params);
        return AjaxResult.success(JSONObject.parseObject(json));
    }

}
